package com.woniu.bank.controller;

import com.woniu.bank.dto.RoleDTO;
import com.woniu.bank.entity.SysRole;
import com.woniu.bank.service.SysPermissionService;
import com.woniu.bank.service.SysRoleService;
import com.woniu.bank.util.Result;
import com.woniu.bank.vo.TreeNodeVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description: TODO
 * @Author: WangHeng
 * @Date: 2023/1/3 14:32
 */
@RestController
@Api(tags = "角色接口",description = "所有的角色接口")
@RequestMapping("roles")
public class RoleController {
    @Autowired
    private SysRoleService roleService;
    @Autowired
    private SysPermissionService permissionService;

    @Autowired
    private RedisTemplate redisTemplate;
    /**
     * @Description 通过用户查询拥有的角色以及查询所有的角色
     *
     */
    @ApiOperation(value = "当前用户拥有的角色",notes ="用户拥有的角色以及所有的角色返回给前端",httpMethod="GET")
    @GetMapping("{uid}")
    public Result getRolesByUser(@PathVariable("uid") Integer uid) {
        List<Integer> checkedRoles = roleService.getRolesByUser(uid);
        List<SysRole> rolesList = roleService.getAllRoles();
        return Result.ok().data("checkedRoles", checkedRoles).data("rolesList", rolesList);
    }

    /**
     * @Description 删除当前用户的角色 赋予新角色
     *
     */
    @ApiOperation(value = "修改用户角色身份",notes ="修改用户的角色",httpMethod="PUT")
    @PutMapping
    public Result updateRoles(@RequestBody RoleDTO roleDTO) {
        int i = roleService.updateRoles(roleDTO);
        if (i != 0) {
            return Result.ok();
        } else {
            return Result.error();
        }
    }

    /**
     * @Description 得到所有的角色列表
     *
     */
    @ApiOperation(value = "得到所有角色身份",notes ="得到所有角色身份",httpMethod="GET")
    @GetMapping("getAllRoles")
    public Result getAllRoles(){
        List<SysRole> roleList = roleService.getAllRoles();
        return Result.ok().data("roleList", roleList);
    }

    /**
     * @Description 得到角色权限树
     *
     */
    @ApiOperation(value = "得到角色权限树",notes ="得到权限树以及默认选中角色拥有的权限",httpMethod="GET")
    @GetMapping("persTree/{rid}")
    public Result getPermissionTree(@PathVariable Integer rid){
        List<TreeNodeVO>  treeList1 = (List<TreeNodeVO>) redisTemplate.opsForValue().get("treeList");
        List<Integer> checkedkeys = roleService.getPermissionByRole(rid);
        if (treeList1 == null) {
            redisTemplate.opsForValue().set("treeList", permissionService.getPermissionByParentId(0));
            return Result.ok().data("checkedKeys", checkedkeys).data("treeList", redisTemplate.opsForValue().get("treeList"));
        }else {
            return Result.ok().data("checkedkeys", checkedkeys).data("treeList", treeList1);
        }
    }

    /**
     * @Description 添加角色
     *
     */
    @ApiOperation(value = "新增角色",notes = "新增角色,返回角色名称即可",httpMethod="POST")
    @PostMapping("addRole")
    public Result addRole(@RequestBody RoleDTO roleDTO) {
        int i = roleService.insertSelective(new SysRole(null, roleDTO.getRoleName()));
        if (i!= 0) {
            redisTemplate.delete("AllRole");
            return Result.error("添加角色成功");
        } else {
            return Result.ok("添加角色失败");
        }
    }
}
